<template>
  <view>
    <view v-if="project">
      <view style="background-color: #ffffff; padding: 30rpx 30rpx 30rpx 30rpx">
        <view class="titleBox text-xl text-black text-bold">
          {{ project.title }}
        </view>
        <view class="timeBox text-df text-gray margin-top-sm margin-bottom-sm">
          <!-- {{article.userId}} - -->
          {{ project.createdTime }} - {{ project.category }}
        </view>
        <image mode="widthFix" :src="project.cover"></image>
        <!-- 中间文章区域 -->
        <view class="contentBox text-lg text-black margin-top-sm margin-tb-lg">
          <!-- <u-parse :content="article.contentMd" :lazy-load="true" ></u-parse> -->
          <c-parse themeColor="#007AFF" :markdown="project.content"></c-parse>
        </view>
      </view>
    </view>
    <view class="cu-bar justify-left bg-white">
      <view class="action border-title">
        <text class="text-lg text-bold text-blue">附件</text>
        <text class="bg-gradual-blue" style="width: 3rem"></text>
      </view>
    </view>
    <view
      style="background-color: white"
      v-if="(project && project.lockStatus == 0) || hasGrant">
      <view v-for="(item, index) in rsrs" :key="index">
        <FileCell
          :fileId="item.id"
          :fileType="item.extName"
          :fileName="item.fileName"
          @confirm="downloadFile"></FileCell>
      </view>
    </view>
    <!--文件锁定-->
    <view style="background-color: white; color: white" v-else>
     <uv-empty mode="permission" ></uv-empty>
    </view>
    <view class="cu-bar" style="height: 150rpx"></view>
    <view class="cu-bar bg-white tabbar foot border shop bottomBox">
      <button class="action" open-type="share">
        <view class="cuIcon-share text-green"></view>
        分享
      </button>
      <button class="action" open-type="contact">
        <view class="cuIcon-service text-green"></view>
        联系我们
      </button>
      <view class="btn-group">
        <button
		open-type="contact"
          style="width: 80%; height: 70rpx"
          @click="toDesign(project.id)"
          class="cu-btn bg-gradual-blue round shadow-blur">
        <view class="cuIcon-attentionfill text-white margin-right-xs"></view> 获得授权/定制
        </button>
      </view>
    </view>
    <view style="width: 100%; height: 50upx"></view>
  </view>
</template>

<script setup>
import { ref } from "vue";
import request from "@/lib/request.js";
import useApp from "@/hooks";
const { $ } = useApp();
import useUserStore from '@/store/module/auth';
const userStore = useUserStore();
const project = ref(null);
const rsrs = ref([])
import { onLoad, onShow } from "@dcloudio/uni-app";
const hasGrant = ref(false)
function queryProject(pid){
	$.get($.api + '/pms/articles/' + pid).then(res=>{
		let t = res.data.data;
		t.cover = t.cover.replace('/dev-api', $.api)
		project.value = t;
		queryRsr()
		queryGrant();
	})
}
// 去定制
function toDesign(pid){
	
}
// 查询资源列表
function queryRsr(){
	$.get($.api + '/pms/projects/' + project.value.id + '/rsr').then(res=>{
		//console.log(res)
		rsrs.value = res.data.data;
	})
}
// 查询授权
function queryGrant(){
	console.log('---授权---')
	$.get($.api + '/pms/project/grant',{pageSize:10,pageNum:1,userId:userStore.userId, projectId:project.value.id}).then(res=>{
		console.log(res)
		let arr =res.data.data;
		if(arr?.length != 0){
			hasGrant.value = true;
		}
	})
}
// 初始化加载
onLoad((options) => {
  let pid = options.id;
  queryProject(pid);
});
// 文件下载
function downloadFile(fileId) {
  console.log(fileId);
  uni.showLoading({
    title: "文件下载中~",
  });
  uni.downloadFile({
    url: $.api + "/ufs/download/"+fileId,
    header: {
      token: uni.getStorageSync("token"),
    },
    success: (res) => {
      console.log(res);
      var filePath = res.tempFilePath;
      let info = res.header["Content-Disposition"];
      console.log(info);
      var fileType = info.substring(info.lastIndexOf(".") + 1); //txt
      console.log(fileType);
      uni.openDocument({
        filePath: filePath,
        fileType: fileType,
        showMenu: true,
        success: function (res) {
          console.log("打开文档成功");
        },
        fail: (r) => {
          console.log(r);
          uni.showToast({
            title: "不支持小程序端查看",
            icon: "none",
          });
        },
      });
    },
    complete: (res) => {
      console.log(res);
      uni.hideLoading();
    },
  });
}
</script>

<style></style>
